package com.ionehe.group.common.uaa.ex.application.rest;

import com.ionehe.group.common.io.Response;
import com.ionehe.group.common.uaa.ex.domain.service.SmsService;
import com.ionehe.group.common.uaa.ex.domain.service.UaaCommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Copyright (c) 2021 杭州铲子科技
 * Date: 2021/11/26
 * Time: 1:25 PM
 *
 * @author 2021年 <a href="mailto:a@ionehe.com">秀</a>
 * 【UaaCommonController】
 */
@RestController
@Slf4j
@Api(value = "uaa-公共服务", tags = "uaa-公共服务")
@RequestMapping("/api/uaa/common")
public class UaaCommonController {
    @Autowired
    private SmsService smsService;
    @Autowired
    private UaaCommonService uaaCommonService;

    @ApiOperation("获取短信验证码")
    @GetMapping("/getSMSVerificationCode")
    public Response<Boolean> getSMSVerificationCode(String mobile, Integer terminal) {
        return smsService.getVerificationCode(mobile, terminal);
    }

    @ApiOperation("获取验证码")
    @GetMapping("/getVerificationCode")
    @ApiImplicitParam(name = "key", value = "验证码唯一标识", dataType = "String", type = "query", required = true)
    public Response<String> getAuthCode(String key, HttpServletRequest request) {
        return uaaCommonService.generateAuthCode(getRequestIp(request) + ":" + key);
    }

    private String getRequestIp(HttpServletRequest request) {
        if (request.getHeader("x-forwarded-for") == null) {
            return request.getRemoteAddr();
        }
        return request.getHeader("x-forwarded-for");
    }
}
